package com.zmj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmj.entity.Book;
import com.zmj.service.BookService;
import com.zmj.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/login")
    @ResponseBody
    public Message login (String username,String password){
        Message message=new Message();
        if ("admin".equals(username)&&"admin".equals(password)){
            message.setCode(1);
        }else {
            message.setCode(0);
            message.setMes("user is not exist ");
        }
        return message;
    }

    @RequestMapping("/tomain")
    public String selAll(HttpServletRequest request){
        int flag=1;
        String num=request.getParameter("pageNum");
        System.out.println(num);
        if (num==null){
            num="1";
        }
        int pageNum=Integer.parseInt(num);
        PageHelper.startPage(pageNum,2);
        List<Book> list=bookService.selAll(flag);
        PageInfo pageInfo=new PageInfo(list);
        List<Book> bookList = pageInfo.getList();
        System.out.println(bookList);
        request.setAttribute("bookList",bookList);
        request.setAttribute("pageInfo",pageInfo);
       return "main";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id,Map<String,Book> map){

        if (id==null||"".equals(id.trim())){
            return "index";
        }else {
            int id1=Integer.parseInt(id);
          Book book=bookService.selOne(id1);
           map.put("book",book);
            return "index";
        }
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public Message addOrUpdate(Book book){

        int id=book.getId();
        Message message=new Message();
        if (id==0){
            boolean flg= bookService.add(book);
            if (flg){
                message.setMes("add success");
                message.setCode(1);
            }else {
                message.setCode(0);
                message.setMes("add fail");
            }
            return message;
        }else {
            boolean flg =bookService.update(book);
            if (flg) {
                message.setCode(1);
                message.setMes("update success");

            } else {
                message.setCode(0);
                message.setMes("update fail");
            }
           return message;
        }

    }

    @RequestMapping("/del")
    @ResponseBody
    public Message del (String id){
        int id1=Integer.parseInt(id);
        Message message=new Message();
        int flag=0;
       boolean flg= bookService.del(flag,id1);
        System.out.println(flg);
        if (flg){
            message.setMes("del success");
            message.setCode(1);
        }else {
            message.setCode(0);
            message.setMes("del fail");
        }
        return  message;
    }

}
